package robotter.bluecambot;

import robotter.bluecambot.configuration;
import robotter.bluecambot.communication;
import robotter.bluecambot.traitementimg;

import java.util.Timer;
import java.util.TimerTask;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.ImageView;

public class camera_handle{
	private SurfaceView preview=null;
	private SurfaceHolder previewHolder=null;
	
	private ImageView preview_seuils=null;
	private ImageView preview_img=null;
	
	private Camera camera=null;
	private Timer proc_timer=null;
	
	private communication comm=null;
	private bluecambot top_class=null;
	
	public camera_handle(bluecambot parent, communication communic){
		Log.d("camera_handle","Creating a camera handle");
		top_class=parent;

		comm=communic;
		
	    /* Screen preview declarations */
		preview=(SurfaceView)top_class.findViewById(R.id.preview);
		previewHolder=preview.getHolder();
		previewHolder.addCallback(surfaceCallback);
		previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

		Log.v("camera_handle","PreviewHolder: OK");
	}
	
	public void finalize(){
		Log.v("camera_handle","Deleting object");
		/* End camera */
		if (camera!=null) {
			Log.v("camera_handle","Closing Camera");
			camera.stopPreview();
			camera.release();
			camera=null;
		}
		
		/* Destroy timer */
		if (proc_timer!=null) {
			Log.v("camera_handle","Closing timer");
			proc_timer.cancel();
		}
	}
	
	
	// ********************************************************
	// ********************************************************
	//                   Callback Section
	// ********************************************************
	// ********************************************************
	
	// Callback for the preview holder 
	SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() {
		public void surfaceCreated(SurfaceHolder holder) {
			while (camera==null){
				try {
					camera=Camera.open();
				}
				catch (Throwable t) {
					Log.e("surfaceCallback","Can't open camera");
				}
				if (camera==null){
					SystemClock.sleep(3000);
					
				}
			}
			try {
				previewHolder.setFixedSize(configuration.cam_img_width, configuration.cam_img_height);
				camera.setPreviewDisplay(previewHolder);
			}
			catch (Throwable t) {
				Log.e("surfaceCallback","Exception in setPreviewDisplay()", t);
			}
			
			/* Timer for repetion of treatment */
			proc_timer=new Timer();
			proc_timer.schedule(TimedAction, 1000, configuration.cam_repeat_time);
			Log.v("camera_handle","Timer: OK");
			

		}
		
		public void surfaceChanged(SurfaceHolder holder,int format, int width,int height) {
			Camera.Parameters parameters=camera.getParameters();
			Log.d("surfaceCallback","Setting width:"+width+" height:"+height);
			width=configuration.cam_img_width;
			height=configuration.cam_img_height;
			parameters.setPreviewSize(width, height);
			parameters.setPictureSize(width, height);
			parameters.setPictureFormat(PixelFormat.JPEG);
			camera.setParameters(parameters);
			camera.startPreview();
		}
		
		public void surfaceDestroyed(SurfaceHolder holder) {
			if (camera!=null) {
				Log.v("camera_handle","Closing Camera");
				camera.stopPreview();
				camera.release();
				camera=null;
			}
			
			/* Destroy timer */
			if (proc_timer!=null) {
				Log.v("camera_handle","Closing timer");
				proc_timer.cancel();
			}
		}
	};
	
	TimerTask TimedAction= new TimerTask(){
	
	// Class of the timer
	//class TimedAction extends TimerTask {
	    public void run() {
			Log.d("TimedAction","Taking photo");
			camera.takePicture(null, null, photoCallback);			
	    }
	};
	
	// Image reception Callback
	Camera.PictureCallback photoCallback=new Camera.PictureCallback() {
		public void onPictureTaken(byte[] data, Camera camera) {
			new PhotoTask().execute(data);
			camera.startPreview();
		}
	};
	
    // Class to process the image datas
	class PhotoTask extends AsyncTask<byte[], String, String> {
		@Override
		protected String doInBackground(byte[]... jpeg) {
			Bitmap bmp_image=BitmapFactory.decodeByteArray(jpeg[0],0,jpeg[0].length);
			traitementimg class_traitement=new traitementimg();
			if (bmp_image==null){ Log.e("PhotoTask", "Can't catch JPEG stream"); return(null); }
			Log.d("PhotoTask","JPEG stream grabbed, len:"+jpeg[0].length);
			Log.d("PhotoTask","Bitmap Info Width:"+bmp_image.getWidth()+" Height:"+bmp_image.getHeight());
			class_traitement.compute_position(bmp_image, comm);
			

			if (class_traitement.seuil_img!=null){
				preview_seuils=(ImageView)top_class.findViewById(R.id.seuilimg);
				preview_seuils.setImageBitmap(class_traitement.seuil_img);
			}
			if (class_traitement.image!=null){
				preview_img=(ImageView)top_class.findViewById(R.id.img_ini);
				preview_img.setImageBitmap(class_traitement.image);
			}

			return(null);
		}
		
	}
	
	
}
